﻿CREATE VIEW [usix].[v_CustomerFile_Update]
AS
SELECT     DM_EXP.[Customer #] UsixID, DM_EXP.[Customer #], CONVERT(int, DM_EXP.[Open Field#01]) [Open Field#01], DM_EXP.Serial#, DM_EXP.[G_L Date], 
DM_EXP.[Open Date], DM_EXP.Name, DM_EXP.Address, DM_EXP.City, DM_EXP.St, DM_EXP.[Zip Code], DM_EXP.[Home Phone #], 
DM_EXP.[Work Phone #], DM_EXP.[Car Year], DM_EXP.Make, DM_EXP.Model, DM_EXP.[Pymnt Schedule], DM_EXP.[# of Payments], 
DM_EXP.[# of PaymentsM], DM_EXP.[Payment Amount], DM_EXP.[Total Payments], DM_EXP.[1st_ Payment Date], DM_EXP.[Last Payment Date], 
DM_EXP.[Next Payment Date], DM_EXP.[# Pmts Remaining], DM_EXP.[# Pmts RemainingM], DM_EXP.[Balance Remaining], 
DM_EXP.[Total Sales Price], DM_EXP.[Finance Charges], DM_EXP.[Status Code], DM_EXP.P_O_E_, DM_EXP.Loan#_Comments, 
DM_EXP.[Primary SSN], DM_EXP.[Amt_ Fin__Disc_ Amt], DM_EXP.[Interest Income], DM_EXP.[Earned Discount], DM_EXP.[Dealer Discount], 
DM_EXP.[Dealer Proceed], DM_EXP.[Dealer Code], DM_EXP.[Principal Balance], DM_EXP.APR, DM_EXP.Mileage, DM_EXP.[Collector Id#], 
DM_EXP.[Discount%], DM_EXP.[Origination State Code], DM_EXP.Portfolio#, DM_EXP.[Sales Rep_], DM_EXP.[Investor Client], DM_EXP.[Date L_C], 
DM_EXP.[Joint Name], DM_EXP.[Joint Address], DM_EXP.[Joint City], DM_EXP.[Joint State], DM_EXP.[Joint Zipcode], DM_EXP.[Joint SSN], 
DM_EXP.[Joint H Phone#], DM_EXP.[Joint W Phone#], DM_EXP.[Primary Cell#], DM_EXP.[Joint Cell#], DM_EXP.[Partial Payment], 
DM_EXP.[NSF Balance], DM_EXP.[Late Chg Bal], DM_EXP.[Apply Late Fee], DM_EXP.Branch#, DM_EXP.CompanyID, 
usix.Bankruptcy_Y_N(DM_EXP.[Bankruptcy Filed Y_N?]) [Bankruptcy Filed Y_N?], DM_EXP.[Bankruptcy Type 07_13?], DM_EXP.CompanyName, 
DM_EXP.[Total Principal], DM_EXP.DPD, DM_EXP.DPD0, DM_EXP.DSLP, DM_EXP.PaymentsMade, DM_EXP.PaymentsMadeM, 
DM_EXP.DownPayment, DM_EXP.[Princ/ChOff Ballance], DM_EXP.[Repo Date], DM_EXP.[A_R Balance], DM_EXP.[Resale Amt_], DM_EXP.Chargeoff, 
DM_EXP.[Final Repo Date], DM_EXP.Retainage, DM_EXP.[Other Chgs Bal], DM_EXP.[Title Received Y_N?], DM_EXP.NDay, 
DM_EXP.[1st_ Payment Date] FirstPaymentDateCalc, CASE WHEN DM_EXP.[1st_ Payment Date] < getdate() 
- 1 THEN dbo.MinNum(dbo.MonthsApart(DM_EXP.[1st_ Payment Date], GETDATE() - 1) + 1, DM_EXP.[# of PaymentsM]) 
ELSE 0 END PaymentsExpectedM, DM_EXP.CompanyID_Raw, DM_EXP.[Simple_Add-On], dbo.clr_VINCheck(DM_EXP.Serial#) VINCheck, 
LPs.Charge LateCharges
, 
LPs.Periods+CASE WHEN LPs.OddDays > 0 THEN 1 ELSE 0 END LatePmtsCount,
CASE [Simple_Add-On] WHEN 'S1' THEN 0 ELSE dbo.Payoff78([Amt_ Fin__Disc_ Amt], [Payment Amount], [# Pmts Remaining], [# of Payments]) 
END RemBalance78, PerDiem
/*INTO usix.t_CustomerFile_Export*/ FROM DataMining.dbo.t_All_Cust_Loan_Repo_PVT AS DM_EXP
LEFT OUTER JOIN ts_LoanPaymentType LPT ON LPT.Code = DM_EXP.[Pymnt Schedule]
OUTER APPLY dbo.LateChargeInfo(DM_EXP.[Total Principal],
	DM_EXP.[Payment Amount],LPT.PaymentsPerYear,DM_EXP.[Next Payment Date],GETDATE(),DM_EXP.[Late Days],DM_EXP.[Late Chg%],DM_EXP.[Late Chg Max]
)LPs
--PaymentsApart(DM_EXP.[Next Payment Date],dbo.Today(GETDATE()),LPT.PaymentsPerYear)LPs






GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane1', @value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End', @level0type = N'SCHEMA', @level0name = N'usix', @level1type = N'VIEW', @level1name = N'v_CustomerFile_Update';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPaneCount', @value = 1, @level0type = N'SCHEMA', @level0name = N'usix', @level1type = N'VIEW', @level1name = N'v_CustomerFile_Update';

